package com.lin.service.MulThread;

import java.util.concurrent.*;

public class CompletableFutureCase {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        long startTime = System.currentTimeMillis();

        //调用用户服务获取用户基本信息
        FutureTask<String> userInfoFutureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("1");
                Thread.sleep(500);
                return "500";
            }
        });
        executorService.submit(userInfoFutureTask);
        System.out.println("2");
        Thread.sleep(300); //模拟主线程其它操作耗时

        FutureTask<String> medalInfoFutureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(500);
                return "200";
            }
        });
        executorService.submit(medalInfoFutureTask);

        System.out.println("3");
        String s300 = userInfoFutureTask.get();//获取个人信息结果
        String s500 = medalInfoFutureTask.get();//获取勋章信息结果
        System.out.println(s300 + "  " + s500);

        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
